package com.cyl.manager.pms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sincere.common.annotation.Log;
import com.sincere.common.core.controller.BaseController;
import com.sincere.common.enums.BusinessType;
import com.cyl.manager.pms.service.ProductService;
import com.cyl.manager.pms.req.ProductReq;
import com.cyl.manager.pms.req.ProductQueryReq;
import com.cyl.manager.pms.resp.ProductResp;
import com.sincere.common.utils.poi.ExcelUtil;
import java.util.List;

/**
 * 商品信息Controller
 * 
 * @author zcc
 * @date 2022-11-28
 */
@Api("商品管理接口列表")
@RestController
@RequestMapping("/pms/product")
@RequiredArgsConstructor
public class ProductController extends BaseController {

    private final ProductService productService;

    @ApiOperation("查询商品列表")
    @PreAuthorize("@ss.hasPermi('pms:product:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<ProductResp>> list(@RequestBody ProductQueryReq query, Pageable page) {
        return ResponseEntity.ok(productService.queryProductByPage(query, page));
    }

    @ApiOperation("所有商品列表")
    @PreAuthorize("@ss.hasPermi('pms:product:list')")
    @PostMapping("/all")
    public ResponseEntity<List<ProductResp>> all() {
        return ResponseEntity.ok(productService.queryAllProduct());
    }

    @ApiOperation("导出商品列表")
    @PreAuthorize("@ss.hasPermi('pms:product:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(ProductQueryReq query) {
        List<ProductResp> list = productService.exportProductList(query);
        ExcelUtil<ProductResp> util = new ExcelUtil<>(ProductResp.class);
        return ResponseEntity.ok(util.writeExcel(list, "商品数据"));
    }

    @ApiOperation("获取商品详细信息")
    @PreAuthorize("@ss.hasPermi('pms:product:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<ProductResp> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(productService.queryProductDetail(id));
    }

    @ApiOperation("新增商品")
    @PreAuthorize("@ss.hasPermi('pms:product:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Boolean> create(@RequestBody ProductReq req) {
        return ResponseEntity.ok(productService.createProduct(req));
    }

    @ApiOperation("修改商品")
    @PreAuthorize("@ss.hasPermi('pms:product:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Boolean> update(@RequestBody ProductReq req) {
        return ResponseEntity.ok(productService.update(req));
    }

    @ApiOperation("删除商品")
    @PreAuthorize("@ss.hasPermi('pms:product:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public ResponseEntity<Boolean> remove(@PathVariable Long id) {
        return ResponseEntity.ok(productService.deleteById(id));
    }
}
